#!/usr/bin/env python3
# Created by han on 2016/6/21
import sys, os, pickle,time
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from conf import db_conf
from module import maker
# STUDENTS_DB = db_conf.SHOOL_DB_BASE
STUDENTS_DB = db_conf.STUDENT_DB_BASE
COURSE_FILE = db_conf.SHOOL_DB_BASE + "/" + "course.db"


def color_msg(color,text,width,fill=" "):
    """
    格式化，可以定义颜色，文字的位置
    :param color: 文字颜色
    :param text: 文字内容
    :param width: 文字位置
    :param fill: 文字填充 default=" "
    :return:
    """
    msg = "\033[31;{0:d}m {1} \033[33;0m".format(color,text)
    text_msg = msg.center(width, fill)
    print(text_msg)


def show(*arg):
    """
    接收菜单给与展示，并返回用户选择
    :param arg:
    :return:
    """
    while True:
        for i in enumerate(arg):
            print(i[0]+1,i[1])
        user_input = input("选择或按【b】退出： ")
        if user_input == "b":
            # break
            return False
        elif user_input and user_input.isdigit() and int(user_input) <= len(arg):
            return user_input
        else:
            msg = "你的输入有误，请重新输入"
            color_msg(31, msg, 40)


def save_obj(name, obj):
    """
    保存学生初始化值
    :param name: 学生名
    :param obj: 学生对象
    :return:
    """
    user_db = STUDENTS_DB + "/"+ name#用户文件
    file_status = os.path.exists(user_db)#用户问价你是否存在
    if not file_status:#不存在
        with open(user_db,'wb') as user_file:
            pickle.dump(obj, user_file)#直接生成这个文件并把对象放进去
        return True
    else:
        # students_obj = pickle.load(open(user_db, "rb"))#取出这个对象
        msg = "已经有这个用户了请重新输入"
        color_msg(31,msg,40)
        return False


def save_stu_obj(name, obj):
    """
    保存学生初始化值
    :param name: 学生名
    :param obj: 学生对象
    :return:
    """
    # user_db = db_conf.DB_BASE + "/" + name  # 用户文件
    user_db = STUDENTS_DB + "/" + name  # 用户文件
    with open(user_db, 'wb') as user_file:
        pickle.dump(obj, user_file)  # 直接生成这个文件并把对象放进去
    return True


def stu_login(user_name):
    """
    学生登陆
    :return:
    """
    # user_file = db_conf.DB_BASE + "/" + user_name#拼出这个用户的文件路径
    user_db = STUDENTS_DB + "/" + user_name#拼出这个用户的文件路径
    if os.path.exists(user_db):
        user_pass = input("请输入密码： ")
        with open(user_db, "rb") as user_file:
            stu_obj = pickle.load(user_file)#通过路径找到用户对象
        if os.path.exists(user_db) and stu_obj.passwd == user_pass:
            # return user_name
            return stu_obj
    else:
        msg = "用户名不存在"
        color_msg(31, msg, 40)
        return False


# def teacher_login():
#     inp = input("请输入用户名")
#     with open()


def chose_obj():
    """
    学生选择课程
    :return:单个课程对象或false
    """
    while True:
        user_chose = input("选择课程,选完请按【b】退出：")
        course_obj_list = maker.chose_course_obj()#获取所有课程的对象列表
        if user_chose == "b":
            return False
        elif user_chose.isdigit and int(user_chose) <= len(course_obj_list):
            course_obj = course_obj_list[int(user_chose)-1]
            return course_obj
        else:
            msg = "输入有误，请重新输入"
            color_msg(31, msg, 40)


def load_stu_obj(user_name):
    """
    提取某个学生的对象
    :param user_name:用户登陆名
    :return: 用户对象的课程列表
    """
    stu_db = STUDENTS_DB + "/" + user_name
    with open(stu_db, "rb") as stu_file:
        stu_obj = pickle.load(stu_file)
    # return stu_obj.timetable
    return stu_obj


def stu_course_chose(stu_obj):
    """
    返回用户选择的课程
    :param stu_obj:
    :return:
    """
    user_chose = input("请输入您的选择或按【b】退出: ")
    # print(stu_obj.timetable)
    if user_chose == "b":
        return "b"
    elif user_chose.isdigit() and int(user_chose) <= len(stu_obj.timetable):#判断用户输入
        # print(stu_obj.timetable)
        stu_course = stu_obj.timetable[int(user_chose)-1]#找出用户选择的那个课程对象
        msg = '在上{0}老师的课'.format(stu_course.teacher.name)
        color_msg(32, msg, 40)
        del stu_obj.timetable[int(user_chose)-1]#因为学生上课，所以就要把上的课从选课单里面删除
        for i in range(0, 101, 2):
            time.sleep(0.05)
            view_bar(i, 100)
        save_stu_obj(stu_obj.name, stu_obj)#再存回文件中
        return stu_course#返回所选的课程对象
    else:
        msg = "您输入有误，请重新输入"
        color_msg(31, msg, 40)
        return False

def view_bar(num,total):
    rate = num/total
    rate_num = int(rate * 100)
    r1 ='\r%s>%d%%'%("="*num, rate_num)
    sys.stdout.write(r1)
    sys.stdout.flush()